package com.dataStructure.tree;

import java.util.Objects;
import java.util.TreeSet;

/**
 * An item has description and part number
 */
public class Item implements Comparable<Item>{
    private String description;
    private int partNumber;


    public Item (String description,int partNumber){
        this.description = description;
        this.partNumber = partNumber;
    }

    public String getDescription() {
        return description;
    }

    public int getPartNumber() {
        return partNumber;
    }

    @Override
    public String toString() {
        return "Item{" +
                "description='" + description + '\'' +
                ", partNumber=" + partNumber +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass())
            return false;
        Item item = (Item) o;
        return partNumber == item.partNumber &&
                Objects.equals(description, item.description);
    }

    @Override
    public int hashCode() {
        return Objects.hash(description, partNumber);
    }

    /**
     * 设置初始自定义treeSet排序
     * 先比较商品序号，再比较商品描述
     * @param item2
     * @return
     */
    public int compareTo(Item item2) {

        int diff = Integer.compare(this.partNumber,item2.partNumber);
        return diff == 0 ? description.compareTo(item2.description) : diff;
    }
}
